'''
Created on May 15, 2010

iur - Universal Web Remote targetted for iphone
Copyright (C) 2010 forteresce@gmail.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

'''
from player.collectionhandler import PlayerCollectionHandler
import sqlite3
from model.album import Album
from model.song import Song
import os
import base64

class BansheeCollectionHandler(PlayerCollectionHandler):
    '''
    The collection handler for Banshee player
    '''
    DB_PATH = os.path.join(os.path.expanduser('~'), '.config', 'banshee-1', 'banshee.db')
    
    def __init__(self):
        self.conn = sqlite3.connect(self.DB_PATH)
        
    def getAlbums(self, startsWith=''):
        albums = []
        try:
            cur = self.conn.cursor()
            
            cur.execute("""
                        SELECT
                            AlbumID, Title
                        FROM
                            CoreAlbums
                        WHERE
                            TitleLowered like '%s%%'
                        """ % startsWith.lower())
            
            for (id, title) in cur:
                if title is None:
                    title = 'Unknown'
                album = Album(id, title)
                albums.append(album)
                
        except Exception, e:
            print str(e)

        return albums
    
    def getSongsInAlbum(self, albumId):
        songs = []
        try:
            cur = self.conn.cursor()
            
            cur.execute("""
                        SELECT
                            t.uri, t.Title, al.title, ar.name
                        FROM
                            CoreTracks t, CoreAlbums al, CoreArtists ar
                        WHERE 
                            t.AlbumID = %s AND t.AlbumID = al.AlbumID AND ar.ArtistID = t.ArtistID
                        ORDER BY
                            t.Title
                        """ % albumId)
            
            for (uri, title, album, artist) in cur:
                if title is None:
                    title = 'Unknown'
                if album is None:
                    album = 'Unknown'
                if artist is None:
                    artist = 'Unknown'
                song = Song(title, artist, album, id=base64.b64encode(uri))
                songs.append(song)
                
        except Exception, e:
            print str(e)

        return songs
